package com.hsnn.medstgmini.base.std.controller.StdDrugcataLogController;

import java.io.BufferedOutputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hsnn.medstgmini.base.std.service.StdDrugcatalogManager;
import com.hsnn.medstgmini.util.DbUtil;
import com.hsnn.medstgmini.util.ExcelUtil;
import com.hsnn.medstgmini.util.Pagination;
/**
 * @category 生产企业 药物目录 控制器
 * @author 韩守松
 * @date 2015年8月26日
 */

@Controller
@RequestMapping( "/produceDrugcatalog" )
public class ProduceDrugCataLogController {
	
	@Autowired
	private StdDrugcatalogManager stdDrugcatalogManager;
	@Autowired
	private HttpServletRequest request;
	
	/**
	 * @category 跳转到生产企业 药物目录列表页面
	 * @author 韩守松
	 * @date   2015年8月26日
	 * @param  @return
	 */
	@RequestMapping( "/list" )
	public String toDrugcatalogList()
	{
		return "/base/std/drugcata/produce/stdDrugcatalogList";
	}

	/**
	 * @category 查询药物目录
	 * @author 韩守松
	 * @date   2015年8月26日
	 * @param  @return
	 */
	@RequestMapping( "/getStdDrugcatalogData" )
	@ResponseBody
	public Pagination getStdDrugcatalogData()
	{
		Pagination page = new Pagination( request );
		DbUtil.jsonToMap( request, page );
		return stdDrugcatalogManager.getStdDrugcatalogList( page ) ;
	}
	@RequestMapping( "/getStdDrugcatalogDatas" )
	@ResponseBody
	public Pagination getStdDrugcatalogDatas()
	{
		Pagination page = new Pagination( request );
		DbUtil.jsonToMap( request, page );
		return stdDrugcatalogManager.getStdDrugcatalogLogLists( page ) ;
	}
	
	/**
	 * @category 去查看药物目录详情 页面
	 * @author 韩守松
	 * @date   2015年8月26日
	 * @param  @return
	 * @throws UnsupportedEncodingException 
	 */
	@RequestMapping( "/toStdDrugcatalogDetailInfo" )
	public String toStdDrugcatalogDetailInfo(String drugcatalogId) throws UnsupportedEncodingException
	{
		request.setAttribute("drugcatalogId", drugcatalogId);
		String drugcatalogName = new String(request.getParameter("drugcatalogName").getBytes("iso8859-1"),"utf-8");
		request.setAttribute("drugcatalogName", drugcatalogName);
		try {
			String drugcatalogNames = new String(request.getParameter("drugcatalogName").getBytes("iso8859-1"),"utf-8");
			request.setAttribute("fileName", drugcatalogNames+".xls");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return("/base/std/drugcata/produce/stdDrugcatalogDetailInfo");
	}
	
	/**
	 * @category 获取药物目录详情数据
	 * @author 韩守松
	 * @date   2015年8月26日
	 * @param  @return
	 */
	@RequestMapping( "/getStdDrugcatalogDetailInfo" )
	@ResponseBody
	public Pagination getStdDrugcatalogDetailInfo(){
		Pagination page = new Pagination(request);
		try {
			DbUtil.jsonToMap(request, page);
			page = stdDrugcatalogManager.getStdDrugcatalogDetailInfo(page);
		} catch (Exception e) {
			e.printStackTrace();
			page.setSuccess(false);
		}
		return page;
	}
	/**
	 * 
	 *@category 导出excel
	 *@author wangbing
	 *@date 2015年6月9日
	 *Pagination
	 */
	@RequestMapping("/exportExcel")
	@ResponseBody
	public Pagination exportExcel(HttpServletResponse response,String fileName){
		//printParams();
		Pagination page = new Pagination();
		String qualityspecClass = request.getParameter("qualityspecClass");
		String qualityspecIndex = request.getParameter("qualityspecIndex");
		String qualityspecCode = request.getParameter("qualityspecCode");
		String qualityspecName = request.getParameter("qualityspecName");
		String qualityspecStatus = request.getParameter("qualityspecStatus");
		String drugcatalogId = request.getParameter("drugcatalogId");
		page.getConditions().put("drugcatalogId", drugcatalogId);
		if(StringUtils.isNotBlank(qualityspecClass)){
			page.getConditions().put("qualityspecClass", qualityspecClass.trim());
		}
		if(StringUtils.isNotBlank(qualityspecIndex)){
			page.getConditions().put("qualityspecIndex", qualityspecIndex.trim());
		}
		if(StringUtils.isNotBlank(qualityspecCode)){
			page.getConditions().put("qualityspecCode", qualityspecCode.trim());
		}
		if(StringUtils.isNotBlank(qualityspecName)){
			page.getConditions().put("qualityspecName", qualityspecName.trim());
		}
		if(StringUtils.isNotBlank(qualityspecStatus)){
			page.getConditions().put("qualityspecStatus", qualityspecStatus.trim());
		}
		
		try {
			response.addHeader("Content-Disposition", "attachment;filename="+new String(fileName.getBytes("utf-8"),"iso8859-1")); 
			OutputStream os= new BufferedOutputStream(response.getOutputStream());  
			 response.setContentType("application/vnd.ms-excel;charset=utf-8");  
			 stdDrugcatalogManager.getDrugcatalogDetailNoPages(page);
			 String title = "药品目录详情";
			 String[] headers = {"分类名称","品种序号","药品编码","通用名","剂型","规格"};
			 String[] alias = {"qualityspecClass","qualityspecIndex","qualityspecCode","qualityspecName","formName","specName"};
			 ExcelUtil.exportExcel(page, os, title, headers, alias);
		} catch (Exception e) {
			page.setSuccess(false);
			e.printStackTrace();
		}
		return page;
	}

}
